package com.feihao.zhou.dao;

import com.feihao.zhou.domain.Student;
import com.feihao.zhou.utils.DruidPoolUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

//读者数据：
public class StudentDao {

    public List<Student> findAllStudent() {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        //创建集合存储学生：
        ArrayList<Student> list = new ArrayList<>();
        try {
            /*//1.注册驱动：
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接：
            connection = DriverManager.getConnection("jdbc:mysql://192.168.26.1:3306/db2", "root", "root");*/
            connection = DruidPoolUtils.getConnection();
            //获取执行对象：
            //sql查询语句：
            String sql = "SELECT * FROM student2";
            preparedStatement = connection.prepareStatement(sql);
            //执行sql：
            resultSet = preparedStatement.executeQuery();


            //处理：
            while (resultSet.next()) {
                Integer id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                Date birthday = resultSet.getDate("birthday");
                Student student = new Student(id, name, age, birthday);
                list.add(student);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidPoolUtils.close(connection, resultSet, preparedStatement);
        }
        return list;
    }

    public void addStudent(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        String name = student.getName();
        Integer age = student.getAge();
        java.util.Date birthday = student.getBirthday();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(birthday);

        try {
            /*//1.注册驱动：
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接：
            connection = DriverManager.getConnection("jdbc:mysql://192.168.26.1:3306/db2", "root", "root");*/
            connection = DruidPoolUtils.getConnection();
            //获取执行对象：
            //sql添加语句：
            String sql = "INSERT INTO student2 VALUES (NULL,?,?,?);";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, name);
            preparedStatement.setInt(2, age);
            preparedStatement.setString(3, format);
            //执行sql：
            preparedStatement.executeUpdate();


            //处理：
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            DruidPoolUtils.close(connection, preparedStatement);
        }
    }

    public void deleteStudent(String id) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            /*//1.注册驱动：
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接：
            connection = DriverManager.getConnection("jdbc:mysql://192.168.26.1:3306/db2", "root", "root");*/
            connection = DruidPoolUtils.getConnection();
            //获取执行对象：
            //sql添加语句：
            String sql = "DELETE FROM student2 WHERE id= ?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, id);

            //执行sql：
            preparedStatement.executeUpdate();


            //处理：
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidPoolUtils.close(connection, preparedStatement);
        }
    }

    public void updateStudent(Student student) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;

        java.util.Date birthday = student.getBirthday();
        String format = new SimpleDateFormat("yyyy-MM-dd").format(birthday);

        try {
            /*//1.注册驱动：
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接：
            connection = DriverManager.getConnection("jdbc:mysql://192.168.26.1:3306/db2", "root", "root");*/
            connection = DruidPoolUtils.getConnection();
            //获取执行对象：
            //sql添加语句：
            String sql = "UPDATE student2 SET NAME =?, age =?, birthday =? WHERE id =?";
            //获取执行对象：
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, student.getName());
            preparedStatement.setInt(2, student.getAge());
            preparedStatement.setString(3, format);
            preparedStatement.setInt(4, student.getSid());

            //执行sql：
            preparedStatement.executeUpdate();


            //处理：
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DruidPoolUtils.close(connection, preparedStatement);
        }
    }
}
